package com.huas.service.serviceimpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huas.common.PageResult;
import com.huas.constant.UserConstant;
import com.huas.dto.TalkDTO;
import com.huas.entity.Article;
import com.huas.entity.LoginUser;
import com.huas.entity.Talk;
import com.huas.entity.User;
import com.huas.exception.BaseException;
import com.huas.mapper.TalkMapper;
import com.huas.mapper.UserMapper;
import com.huas.service.TalkService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class TalkServiceImpl implements TalkService {
    @Resource
    private TalkMapper talkMapper;
    @Resource
    private UserMapper userMapper;
    @Override
    public PageResult page(Integer page, Integer pageSize,Integer status) {
        //PageHelper设置分页参数
        PageHelper.startPage(page,pageSize);

        Page<Talk> p = talkMapper.page(status);

        List<Talk> talkList = p.getResult();

        //遍历集合得到每一个实体类对象
        for (Talk talk : talkList) {
            //得到发布人id查询用户得到对应name名称
            Integer authorId = talk.getCreateBy();
            User user = userMapper.findById(authorId);
            if(user != null){
                talk.setAuthor(user.getName());
            }
        }
        return new PageResult(p.getTotal(),p.getResult());
    }

    @Override
    public void insert(Talk talk) {
        //获取当前用户的用户id
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        Integer id = loginUser.getUser().getId();

        //补充基础信息
        talk.setCreateBy(id);
        talk.setCreateTime(LocalDateTime.now());
        talk.setUpdateTime(LocalDateTime.now());

        talkMapper.reset();
        talkMapper.insert(talk);
    }

    @Override
    public void updateTalk(TalkDTO talkDTO) {
        Talk talk = new Talk();
        BeanUtils.copyProperties(talkDTO,talk);
        //重置修改时间
        talk.setUpdateTime(LocalDateTime.now());
        talkMapper.update(talk);
    }

    @Override
    public void delete(Integer id) {
        talkMapper.delete(id);
    }

    @Override
    public void deleteBatch(List<Integer> ids) {
        if(ids.isEmpty()){
            throw new BaseException("未选定删除信息");
        }
        talkMapper.deleteBatch(ids);
    }

    @Override
    public List<Talk> selectTalkList() {
        List<Talk> talkList =talkMapper.selectTalkList();
        //遍历集合得到每一个实体类对象
        for (Talk talk : talkList) {
            //得到发布人id查询用户得到对应name名称
            Integer authorId = talk.getCreateBy();
            User user = userMapper.findById(authorId);
            if(user != null){
                talk.setAuthor(user.getName());
            }
        }
        return talkList;
    }
}
